package cn.yr.clock.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.yr.clock.domain.entity.Engine;
import cn.yr.clock.domain.vo.EnginePrizeVo;
import cn.yr.clock.handler.CacheUserHandler;
import cn.yr.clock.handler.EngineHandler;
import cn.yr.clock.service.EngineService;
import cn.yr.clock.domain.vo.PageVo;
import cn.yr.common.exception.ret.Ret;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author Lang
 * @email 1102076808@qq.com
 * @date 2021-09-25 11:13:41
 */
@RestController
@RequestMapping("/engine")
public class EngineController {
    @Autowired
    private EngineService engineService;

    @Autowired
    private EngineHandler engineHandler;

    @Autowired
    private CacheUserHandler cacheUserHandler;

    /**
     * 打卡数
     *
     * @return
     */
    @RequestMapping("/countClock")
    public Ret countClock() {
        Long userId = cacheUserHandler.getUser().getUserId();
        Integer sumClock = engineHandler.sumClock(userId);
        Integer continuousClock = engineHandler.continuousClock(userId);
        Integer cycleNum = engineHandler.cycleNum(userId);
        Map<Object, Object> map = MapUtil.builder().put("sumClock", sumClock)
                .put("continuousClock", continuousClock).put("cycleNum", cycleNum).map();
        return Ret.success(map);
    }

    @RequestMapping("/listEngine")
    public Ret listEngine(@RequestBody EnginePrizeVo enginePrizeVo) {
        Long userId = cacheUserHandler.getUser().getUserId();
        enginePrizeVo.setUserId(userId);
        List<EnginePrizeVo> enginePrizeVos = engineService.listEngine(enginePrizeVo);
        enginePrizeVos.forEach(item -> {
            if (ObjectUtil.isNull(item.getStatus())) {
                item.setStatus(3);
            }
        });
        return Ret.success(enginePrizeVos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Ret list(@RequestBody PageVo<Engine> pageVo) {
        PageVo<Engine> pageEngine = engineService.page(pageVo, new QueryWrapper<>());
        return Ret.success(pageEngine);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{engineId}")
    public Ret info(@PathVariable("engineId") Long engineId) {
        Engine engine = engineService.getById(engineId);
        return Ret.success(engine);
    }

    /**
     * 保存
     */
    @RequestMapping("/add")
    public Ret save(@Validated @RequestBody Engine engine) {
        engineService.save(engine);
        return Ret.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    public Ret update(@Validated @RequestBody Engine engine) {
        engineService.updateById(engine);
        return Ret.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/remove")
    public Ret delete(@RequestBody Long[] engineIds) {
        engineService.removeByIds(Arrays.asList(engineIds));
        return Ret.success();
    }

    /**
     * 情况缓存
     * @return
     */
    @RequestMapping("clearCache")
    public Ret clearCache(){
        engineHandler.clearCache();
        return Ret.success();
    }

}
